import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/widgets/lf_text_filed.dart';

import '../../../config/lf_config.dart';
import '../../../res/lf_fonts.dart';
import '../../../res/lf_text_style.dart';
import '../../../res/strings/lf_strings.dart';
import '../../../widgets/lf_app_bar.dart';
import '../../../widgets/lf_button.dart';
import '../../lf_payment/lf_payment_logic.dart';
import 'lf_change_username_logic.dart';

class LfChangeUsernamePage extends StatefulWidget {
  const LfChangeUsernamePage({super.key});

  @override
  State<LfChangeUsernamePage> createState() => _LfChangeUsernamePageState();
}

class _LfChangeUsernamePageState extends State<LfChangeUsernamePage> {
  final logic = Get.put(LfChangeUsernameLogic());

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: lfAppBar(
              leadingCallback: () {
                Get.back();
              },
              title: strChangeUsername.tr),
          body: Padding(
            padding: EdgeInsets.symmetric(horizontal: 15.w),
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  10.h.sbh,
                  Text(
                    strYourCurrentUsername.tr,
                    style: lfTextStyle(sp: 18.sp, fontFamily: fontBold),
                  ),
                  6.h.sbh,
                  Container(
                    width: ScreenUtil().screenWidth,
                    decoration: BoxDecoration(
                        color: color50F2A7BE, borderRadius: 5.r.radius),
                    padding:
                        EdgeInsets.symmetric(vertical: 10.h, horizontal: 15.w),
                    child: Text(
                      logic.authService.currentAccount!.username.toUpperCase(),
                      style: lfHintStyle,
                    ),
                  ),
                  30.h.sbh,
                  Text(
                    strNewUsername.tr,
                    style: lfTextStyle(sp: 18.sp, fontFamily: fontBold),
                  ),
                  10.h.sbh,
                  LfTextFiled(
                    textEditingController: logic.textEditingController,
                    onChanged: (value) {
                      logic.newName.value = value.trim();
                    },
                    inputFormatters: [
                      LengthLimitingTextInputFormatter(maxName),
                      FilteringTextInputFormatter.allow(RegExp('[A-Za-z0-9]')),
                    ],
                    focusNode: logic.focusNode,
                    isPassword: false,
                    hintText: strEnterYourNewUsername.tr,
                  ),
                  45.h.sbh,
                  Obx(() => LfButton(
                      enabled: logic.newName.value.trim().length >= minName &&
                          (logic.newName.value.trim().toUpperCase() !=
                              logic.authService.currentAccount!.username
                                  .toUpperCase()),
                      text: strConfirm.tr,
                      onPress: () {
                        if (logic.authService.currentAccount!.isGold()) {
                          logic.changeUsername();
                        } else {
                          toPayment();
                        }
                      }))
                ],
              ),
            ),
          ),
        ),
        onWillPop: () async {
          Get.back();
          return false;
        });
  }
}
